package com.chinasoft.mall.activity.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.activity.subActivity.HomePageActivity;
import com.chinasoft.mall.activity.subActivity.more.HomePageMoreActivity;
import com.chinasoft.mall.model.response.ModelWeiboBean;
import com.chinasoft.mall.model.response.PushMessageBean;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.widget.NetImageView;
import com.chinasoft.mall.widget.dialog.ShowImageDialog;

@SuppressLint("UseSparseArrays")
public class ModelList2Adapter<T> extends BaseAdapter {
	private List<T> dds;
	private LayoutInflater inflater;
	private ViewHolder _vh;
	private MainActivity MainActivity;

	public ModelList2Adapter(MainActivity MainActivity, List<T> crs) {
		inflater = LayoutInflater.from(MainActivity);
		this.MainActivity = MainActivity;
		this.dds = crs;
	}

	public void setData(List<T> crs) {
		this.dds = crs;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (dds == null) {
			return 0;
		} else {
			return dds.size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (dds == null) {
			return null;
		} else {
			return dds.get(position);
		}
	}

	private void initView(List<T> lst) {

	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private Map<Integer, View> wids = new HashMap<Integer, View>();

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = wids.get(position);

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.sy_modeldetaillv, null);
			_vh = new ViewHolder();
			_vh.modelimg = (NetImageView) convertView
					.findViewById(R.id.modelimg);
			_vh.time = (TextView) convertView.findViewById(R.id.time);
			_vh.intro = (TextView) convertView.findViewById(R.id.intro);

			Object _dd = dds.get(position);
			if (_dd instanceof ModelWeiboBean) {
				ModelWeiboBean dd = (ModelWeiboBean) _dd;
				String imageurl = StringUtil.modelWeiboUrl(dd.getMODEL_ID(),
						dd.getPUB_DATE());
				_vh.modelimg.setDownload(imageurl);
				_vh.time.setText(dd.getPUB_DATE());
				_vh.intro.setText(dd.getCONTENT());
			} else if(_dd instanceof PushMessageBean){
				PushMessageBean dd = (PushMessageBean) _dd;
				_vh.time.setText(dd.getSTARTTIME());
				_vh.intro.setText(dd.getCONTENT());
			}
			convertView.setOnClickListener(new onClickImpl(MainActivity, dds,
					position));
			_vh.modelimg.setOnClickListener(new onClickImageImpl(MainActivity,
					_vh.modelimg));
			convertView.setTag(_vh);
			wids.put(position, convertView);
		}
		return convertView;
	}

	/**
	 * 跳转到通道详情
	 * 
	 * @author Administrator
	 * 
	 */
	class onClickImpl implements View.OnClickListener {
		private List<T> crs;
		private int position;

		// private MainActivity mainActivity;

		public onClickImpl(MainActivity MainActivity, List<T> crs, int position) {
			this.crs = crs;
			this.position = position;
			// this.mainActivity = MainActivity;
		}

		@Override
		public void onClick(View v) {
			T scd = crs.get(position);
			if (scd != null) {
				// mainActivity.setCurrentActivity(SyModelDetailActivity.class,
				// scd.getGOODS_KEYWORDS(),scd);
			}
		}
	}

	class onClickImageImpl implements View.OnClickListener {
		private NetImageView crs;
		private MainActivity mainActivity;

		public onClickImageImpl(MainActivity MainActivity, NetImageView crs) {
			this.crs = crs;
			this.mainActivity = MainActivity;
		}

		@Override
		public void onClick(View v) {
			ShowImageDialog sid = new ShowImageDialog(mainActivity,
					crs.getDownloadurl());
			sid.show();
		}
	}

	class ViewHolder {
		public NetImageView modelimg;
		public TextView time;
		public TextView intro;
	}
}
